package net.myspring.common.utils;

import com.google.common.collect.Lists;

import java.util.List;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    public static String toString(Object obj) {
        if (obj == null) {
            return "";
        } else {
            return String.valueOf(obj);
        }
    }


    public static <E> List<E> getSplitList(String str, String splitter) {
        List<E> list = Lists.newArrayList();
        if (isNotBlank(str)) {
            String[] arr = str.split(splitter);
            for (String item : arr) {
                if (isNotBlank(item) && !list.contains(item.trim())) {
                    E value = (E) item.trim();
                    list.add(value);
                }
            }
        }
        return list;
    }

    /**
     * 把中文转成Unicode码
     *
     * @param str
     * @return
     */
    public static String chinaToUnicode(String str) {
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            int chr1 = (char) str.charAt(i);
            // 汉字范围 \u4e00-\u9fa5 (中文)
            if (chr1 >= 19968 && chr1 <= 171941) {
                result += "\\u" + Integer.toHexString(chr1);
            } else {
                result += str.charAt(i);
            }
        }
        return result;
    }

    public static String getFormatId(Long id, String prefix) {
        return getFormatId(id, prefix, "000000000000");
    }

    public static String getFormatId(Long id, String prefix, String format) {
        if (id == null) {
            return "";
        } else {
            java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat(format);
            return prefix + decimalFormat.format(id);
        }
    }
}
